<template>
  <div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    />
    <div class="doumee-table-buttons">
      <el-button type="primary" @click="toAdd(0)">{{
        translate("add")
      }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="translate('venueName')"
        prop="venueName"
        min-width="150"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('discountType')"
        prop="type"
        min-width="120"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="scope.row.type == '1'">{{ translate("shichang") }}</span>
          <span v-else>{{ translate("other") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('yhsc')"
        prop="duration"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('everymemLimit')"
        prop="peoLimit"
        min-width="130"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="scope.row.peoXl == '1'">{{ scope.row.peoLimit }}</span>
          <span v-else>{{ translate("noLimit") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('totalGiveNum')"
        prop="totalNumber"
        min-width="100"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="scope.row.xl == '1'">{{ scope.row.totalNumber }}</span>
          <span v-else>{{ translate("noLimit") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('createTime')"
        prop="createTime"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('yffsl')"
        prop="issuedNumber"
        min-width="100"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ scope.row.issuedNumber || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('status')"
        min-width="100"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- :disabled="meta.indexOf('ComboStatus') == -1" -->
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="statusAction(scope.row.id, scope.row.status)"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="translate('handle')"
        min-width="200"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button type="text" @click="toAdd('1', scope.row.id)">{{
              translate("edit")
            }}</el-button>
            <el-button
              type="text"
              class="red"
              @click="toDelete(scope.row.id)"
            >{{ translate("del") }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <!-- 新增 编辑 -->
    <el-dialog
      :close-on-click-modal="modalDialog"
      :lose-on-press-escape="modalDialog"
      :title="addEditType ? translate('bjtcyh') : translate('xztcyh')"
      :visible.sync="shopDia"
      width="600px"
      @closed="cancelDia"
    >
      <el-form ref="ruleForm" :model="addForm" label-width="auto" :rules="rules">
        <el-form-item :label="translate('chooseVenue')" prop="venueId">
          <el-select
            v-model="addForm.venueId"
            :placeholder="translate('placechooseVenue')"
            style="width: 100%"
          >
            <el-option
              v-for="item in relation"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="translate('yhsc')" prop="duration">
          <el-input
            v-model="addForm.duration"
            :placeholder="translate('qsr')"
            oninput="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
            maxlength="10"
          >
            <template #append>{{ translate("hours") }}</template>
          </el-input>
        </el-form-item>
        <el-form-item :label="translate('sfxl')" prop="peoXl">
          <el-radio-group v-model="addForm.peoXl">
            <el-radio label="0">{{ translate("limitNot") }}</el-radio>
            <el-radio label="1">{{ translate("limitNum") }}</el-radio>
          </el-radio-group>
          <el-input
            v-if="addForm.peoXl == 1"
            v-model="addForm.peoLimit"
            :placeholder="translate('qsr')"
            oninput="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
            maxlength="10"
            class="w200 ml10"
          >
            <template #append>{{ translate("part") }}</template>
          </el-input>
          <div class="placeholder9">{{ translate("xzdghymrlq") }}</div>
        </el-form-item>
        <el-form-item :label="translate('zffl')" prop="totalNumber">
          <el-input
            v-model="addForm.totalNumber"
            :placeholder="translate('qsr')"
            oninput="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
            maxlength="10"
          >
            <template #append>{{ translate("part") }}</template>
          </el-input>
          <div class="placeholder9">{{ translate("wkbsbxzffsl") }}</div>
        </el-form-item>
        <el-form-item :label="translate('status')" prop="status">
          <el-switch
            v-model="addForm.status"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
        <el-form-item :label="translate('bz')" prop="remark">
          <el-input
            v-model="addForm.remark"
            :placeholder="translate('qsr')"
            maxlength="200"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button v-preventReClick @click="cancelDia">{{
          translate("cancel")
        }}</el-button>
        <el-button
          :loading="publishLoading"
          type="primary"
          @click="confirmSub"
        >{{ translate("enter") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pdList, pdDisable, pdEdit, pdDel, venueList, pdEditDetail, pdAdd } from '@/api'
import { parseTime } from '@/utils'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
export default {
  name: 'NoticeVenue',
  components: {
    Pagination
  },
  data() {
    return {
      queryFormConfig,
      modalDialog: this.$store.state.app.dialog,
      publishLoading: false,
      listLoading: false,
      list: [],
      meta: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {},
      relation: [],
      addEditType: 0,
      shopDia: false,
      addForm: {
        peoXl: '0',
        status: '0'
      },
      rules: {
        duration: [
          { required: true, message: this.translate('yhscPlace'), trigger: 'blur' }
        ],
        venueId: [
          { required: true, message: this.translate('placechooseVenue'), trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getVenueList()
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    loadingTable() {
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    },
    statusAction(id, status) {
      pdDisable({ param: { id, status }}).then(res => {
        this.getList()
        if (res.errorCode === '000000') {
          this.$message.success(status === 0 || status === '0' ? this.translate('jyScs') : this.translate('qyScs'))
        }
      }, () => {
        this.getList()
      })
    },
    // 获取场馆列表
    getVenueList() {
      venueList({
        param: {}
      }).then(res => {
        if (res.errorCode !== '000000') return
        this.relation = res.recordList
        const arr = res.recordList
        this.queryFormConfig.formItems[0].options = arr
      })
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      this.listLoading = true
      pdList({ pagination, param }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList
          this.total = res.totalCount
          if (res.totalCount && res.recordList.length === 0 && pagination.page > 1) {
            this.getList(Math.ceil(res.totalCount / pagination.rows))
          }
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    },
    // 新增/编辑消息
    toAdd(type, id) {
      this.addEditType = type
      this.shopDia = true
      if (id) {
        pdEditDetail({ param: { id }}).then(res => {
          if (res.errorCode === '000000') {
            this.addForm = res.record
          }
        })
      } else {
        this.addForm = {
          peoXl: '0',
          status: '0'
        }
      }
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate()
      })
    },
    // 取消新增/编辑
    cancelDia() {
      this.shopDia = false
    },
    // 确认新增/编辑
    confirmSub() {
      const that = this
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const { addEditType } = this
          const param = { ...this.addForm }
          if (param.peoXl && param.peoXl === '1') {
            if (!param.peoLimit) {
              return this.$message.warning(this.translate('limitNumPlace'))
            }
          }
          that.publishLoading = true
          if (addEditType) {
            pdEdit({ param }).then(res => {
              if (res.errorCode !== '000000') return
              that.publishLoading = false
              that.shopDia = false
              that.getList()
            }).catch((er) => {
              that.publishLoading = false
              console.log(er)
            })
          } else {
            pdAdd({ param }).then(res => {
              if (res.errorCode !== '000000') return
              that.publishLoading = false
              that.shopDia = false
              that.getList()
            }).catch((er) => {
              that.publishLoading = false
              console.log(er)
            })
          }
        } else {
          that.publishLoading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    // 删除
    toDelete(id) {
      this.$confirm(this.translate('couponDelPlace'), this.translate('ts'), {
        confirmButtonText: this.translate('qd'),
        cancelButtonText: this.translate('cancel'),
        type: 'warning'
      })
        .then(() => {
          pdDel({ param: { id }}).then(ress => {
            if (ress.errorCode === '000000') {
              this.getList()
            }
          })
        })
        .catch(() => {
          // this.$message.info(this.translate('canceledDel'))
        })
    }
  }
}
</script>

